package v1

import (
	"hr/api/dto"
	"hr/api/middleware"
	"hr/db"
	"hr/utils"
	"strings"

	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

// GetSalary
// @Summary 获取薪资列表
// @Tags 薪资相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param pageNo query string true "页码"
// @Param pageSize query string true "页大小"
// @Param name query string false "名称"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/salary [get]
func GetSalary(c *gin.Context) {
	pageNoStr := c.Query("pageNo")
	pageSizeStr := c.Query("pageSize")
	name := c.Query("name")

	var pageNo, pageSize int
	if !utils.IsStringEmpty(pageNoStr) {
		pageNo = cast.ToInt(pageNoStr)
	}
	if !utils.IsStringEmpty(pageSizeStr) {
		pageSize = cast.ToInt(pageSizeStr)
	}
	paging := &dto.Pagination{
		PageNo:   pageNo,
		PageSize: pageSize,
	}

	d, err := db.GetSalaryPaging(*paging, name)

	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}
	utils.EchoSuccess(c, d, "请求成功")
}

// ImportSalary
// @Summary 导入薪资表
// @Tags 薪资相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param file formData file true "信息"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/salary/import [post]
func ImportSalary(c *gin.Context) {
	date := c.PostForm("date")
	if date == "" {
		utils.EchoError(c, "缺少必要参数")
		return
	}
	file, _, err := c.Request.FormFile("file")
	if err != nil {
		utils.EchoError(c, "缺少必要参数")
		return
	}
	xlsx, err := excelize.OpenReader(file)
	if err != nil {
		utils.EchoError(c, "文件错误")
		return
	}

	id, _ := middleware.GetCurrentUserId(c)
	var salary []db.Salary
	for _, sheet := range xlsx.GetSheetMap() {
		for rIndex, rows := range xlsx.GetRows(sheet) {
			if rIndex < 2 {
				continue
			}
			sa := db.Salary{
				DateMonth: date,
				Name:      strings.Trim(rows[1], " "),
				WorkDays:  rows[2],
				RealDays:  rows[3],
				BasePay:   rows[4],
				PostPay:   rows[5],
				WorkPay:   rows[6],
				WorkAge:   rows[7],
				DuePay:    rows[8],
				Excellent: rows[9],
				OtherPay:  rows[10],
				Insurance: rows[11],
				Merits:    rows[12],
				UnionFee:  rows[13],
				RealPay:   rows[14],
				Remark:    rows[15],
			}

			salary = append(salary, sa)
		}
	}

	for _, sl := range salary {
		if sl.Name != "" {
			db.CreateSalary(sl, cast.ToString(id))
		}
	}
	utils.EchoSuccess(c, nil, "导入成功")
}

// DeleteSalary
// @Summary 删除薪资
// @Tags 薪资相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param id path string true "主键ID"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/salary/{id} [delete]
func DeleteSalary(c *gin.Context) {
	idStr := c.Param("id")
	if utils.IsStringEmpty(idStr) {
		utils.EchoError(c, "缺少ID")
		return
	}
	err := db.DeleteSalary(cast.ToUint(idStr))
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, nil, "删除成功")
}

// GetSalaryByUser
// @Summary 获取用户薪资列表
// @Tags 薪资相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param pageNo query string true "页码"
// @Param pageSize query string true "页大小"
// @Param userId query string false "用户id"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/salary/user [get]
func GetSalaryByUser(c *gin.Context) {
	pageNoStr := c.Query("pageNo")
	pageSizeStr := c.Query("pageSize")
	id, _ := middleware.GetCurrentUserId(c)

	var pageNo, pageSize int
	if !utils.IsStringEmpty(pageNoStr) {
		pageNo = cast.ToInt(pageNoStr)
	}
	if !utils.IsStringEmpty(pageSizeStr) {
		pageSize = cast.ToInt(pageSizeStr)
	}
	paging := &dto.Pagination{
		PageNo:   pageNo,
		PageSize: pageSize,
	}

	d, err := db.GetSalaryPagingByUser(*paging, cast.ToString(id))

	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}
	utils.EchoSuccess(c, d, "请求成功")
}
